/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package piglatinator;
import java.util.*;
import java.io.*;

/**
 *
 * @author Ronald
 */
public class Translation
{
    String userInput;

    public Translation()
    {

    }
    public Translation(String newUserInput)
    {
        this.userInput = newUserInput;
    }
    public void beginTranslation()
    {
        String again = "";
        do{
            BufferedReader reader;
            reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("");
            System.out.println("What do you need to translate? ");
            try
            {
                userInput = reader.readLine();
                System.out.println("");
                System.out.println("Your Input: "+userInput.toLowerCase());
                String pigLatin = breakSentence(userInput);
                System.out.println("Pig Latin Output: "+pigLatin);
                System.out.println("");
                System.out.println("Translate another sentence (y/n)? ");
                again = reader.readLine();
            } catch (IOException ioe)
            {
                System.out.println("An unexpected error occured.");
            }
        } while (again.equalsIgnoreCase("y"));
    }
    private boolean beginsWithVowel (String userInput)
    {
        String vowels = "aeiouy";
        char letter = userInput.charAt(0);
        return (vowels.indexOf(letter) != -1);
    }
    private boolean beginsWithConsonant (String userInput)
    {
        String consonant = "bcdfghjklmnpqrstvwxz";
        char letter = userInput.charAt(0);
        return (consonant.indexOf(letter) != -0);
    }
    public String breakSentence(String sentence)
    {
        String output = "";
        userInput = sentence;
        sentence = sentence.toLowerCase();
        StringTokenizer st = new StringTokenizer(sentence);
        while (st.hasMoreTokens())
        {
            output += breakWord (st.nextToken());
            output += " ";
        }
        return output;
    }
    public String breakWord(String word)
    {
        String output = "";
        if(beginsWithVowel(word))
        {
            output = word + "way";
        } else
        {
            if(beginsWithConsonant(word))
            {
                output = word.substring(2) + word.substring(0,2) + "ay";
            } else
            {
                output = word.substring(1) + word.charAt(0) + "ay";
            }
        }
        return output;
    }
    public void Index()
    {
       System.out.println("");
       System.out.println("****************************************************");
       System.out.println("PIG LATINATOR V1.0");
       System.out.println("****************************************************");
    }
}
